package cc.catface.mess.message.activemq.queue;

import cc.catface.mess.message.activemq.ActiveMQUtil;
import cn.hutool.core.util.RandomUtil;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class QueueConsumerB {
    private static final String url = "tcp://127.0.0.1:61616";
    private static final String topicName = "queue_style";

    private static final String consumerName = "consumer-" + RandomUtil.randomString(5);

    public static void main(String[] args) throws JMSException {
        ActiveMQUtil.checkServer();
        System.out.printf("消费者启动了:%s%n", consumerName);
        ConnectionFactory factory = new ActiveMQConnectionFactory(url);
        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue(topicName);
        MessageConsumer consumer = session.createConsumer(destination);
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message arg0) {
                TextMessage textMessage = (TextMessage) arg0;
                try {
                    System.out.println(consumerName + "=>接收消息:" + textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        //consumer.close();
        //session.close();
        //connection.close();
    }
}
